<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>calling_methods - RDoc Documentation</title>


<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>

<script src="../js/jquery-3.2.0.min.js"></script>

<script src="../js/vue.min.js"></script>
<script src="../js/js.cookie.min.js"></script>

<link href="../css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="../css/rdoc.css" rel="stylesheet">
<link href="../css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../";
  var index_rel_prefix = "../";
  var darkModeCsseHref = "../css/rdoc-dm.css"
  var defaultModeCssHref = "../css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="file">
  <!-- this is page.html -->
  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->


 <div class='wrapper hdiv'>

 


<nav id='vapp' role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="../index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="../table_of_contents.html#pages">Pages</a>
    <a href="../table_of_contents.html#classes">Classes</a>
    <a href="../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#label-Calling+Methods">Calling Methods</a>
    <li><a href="#label-Receiver">Receiver</a>
    <li><a href="#label-Chaining+Method+Calls">Chaining Method Calls</a>
    <li><a href="#label-Safe+Navigation+Operator">Safe Navigation Operator</a>
    <li><a href="#label-Arguments">Arguments</a>
    <li><a href="#label-Positional+Arguments">Positional Arguments</a>
    <li><a href="#label-Default+Positional+Arguments">Default Positional Arguments</a>
    <li><a href="#label-Keyword+Arguments">Keyword Arguments</a>
    <li><a href="#label-Block+Argument">Block Argument</a>
    <li><a href="#label-Block+Local+Arguments">Block Local Arguments</a>
    <li><a href="#label-Array+to+Arguments+Conversion">Array to Arguments Conversion</a>
    <li><a href="#label-Hash+to+Keyword+Arguments+Conversion">Hash to Keyword Arguments Conversion</a>
    <li><a href="#label-Proc+to+Block+Conversion">Proc to Block Conversion</a>
    <li><a href="#label-Method+Lookup">Method Lookup</a>
  </ul>
</div>

  <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>

  <div id="project-metadata">
   <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
    
<div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
    <li><a href="../NEWS/NEWS-1_8_7.html">NEWS-1.8.7</a>
    <li><a href="../NEWS/NEWS-1_9_1.html">NEWS-1.9.1</a>
    <li><a href="../NEWS/NEWS-1_9_2.html">NEWS-1.9.2</a>
    <li><a href="../NEWS/NEWS-1_9_3.html">NEWS-1.9.3</a>
    <li><a href="../NEWS/NEWS-2_0_0.html">NEWS-2.0.0</a>
    <li><a href="../NEWS/NEWS-2_1_0.html">NEWS-2.1.0</a>
    <li><a href="../NEWS/NEWS-2_2_0.html">NEWS-2.2.0</a>
    <li><a href="../NEWS/NEWS-2_3_0.html">NEWS-2.3.0</a>
    <li><a href="../NEWS/NEWS-2_4_0.html">NEWS-2.4.0</a>
    <li><a href="../NEWS/NEWS-2_5_0.html">NEWS-2.5.0</a>
    <li><a href="../NEWS/NEWS-2_6_0.html">NEWS-2.6.0</a>
    <li><a href="../NEWS/NEWS-2_7_0.html">NEWS-2.7.0</a>
    <li><a href="../NEWS/NEWS-3_0_0_md.html">NEWS-3.0.0</a>
    <li><a href="../NEWS/NEWS-3_1_0_md.html">NEWS-3.1.0</a>
    <li><a href="../NEWS/NEWS-3_2_0_md.html">NEWS-3.2.0</a>
    <li><a href="../bsearch_rdoc.html">bsearch</a>
    <li><a href="../bug_triaging_rdoc.html">bug_triaging</a>
    <li><a href="../case_mapping_rdoc.html">case_mapping</a>
    <li><a href="../character_selectors_rdoc.html">character_selectors</a>
    <li><a href="../command_injection_rdoc.html">command_injection</a>
    <li><a href="../contributing_md.html">contributing</a>
    <li><a href="../contributing/building_ruby_md.html">building_ruby</a>
    <li><a href="../contributing/documentation_guide_md.html">documentation_guide</a>
    <li><a href="../contributing/glossary_md.html">glossary</a>
    <li><a href="../contributing/making_changes_to_ruby_md.html">making_changes_to_ruby</a>
    <li><a href="../contributing/making_changes_to_stdlibs_md.html">making_changes_to_stdlibs</a>
    <li><a href="../contributing/reporting_issues_md.html">reporting_issues</a>
    <li><a href="../contributing/testing_ruby_md.html">testing_ruby</a>
    <li><a href="../dig_methods_rdoc.html">dig_methods</a>
    <li><a href="../distribution_md.html">distribution</a>
    <li><a href="../dtrace_probes_rdoc.html">dtrace_probes</a>
    <li><a href="../encodings_rdoc.html">encodings</a>
    <li><a href="../extension_ja_rdoc.html">extension.ja</a>
    <li><a href="../extension_rdoc.html">extension</a>
    <li><a href="../fiber_md.html">fiber</a>
    <li><a href="../format_specifications_rdoc.html">format_specifications</a>
    <li><a href="../globals_rdoc.html">globals</a>
    <li><a href="../implicit_conversion_rdoc.html">implicit_conversion</a>
    <li><a href="../keywords_rdoc.html">keywords</a>
    <li><a href="../maintainers_md.html">maintainers</a>
    <li><a href="../marshal_rdoc.html">marshal</a>
    <li><a href="../memory_view_md.html">memory_view</a>
    <li><a href="../optparse/argument_converters_rdoc.html">argument_converters</a>
    <li><a href="../optparse/creates_option_rdoc.html">creates_option</a>
    <li><a href="../optparse/option_params_rdoc.html">option_params</a>
    <li><a href="../optparse/tutorial_rdoc.html">tutorial</a>
    <li><a href="../packed_data_rdoc.html">packed_data</a>
    <li><a href="../ractor_md.html">ractor</a>
    <li><a href="../regexp_rdoc.html">regexp</a>
    <li><a href="../regexp/methods_rdoc.html">methods</a>
    <li><a href="../regexp/unicode_properties_rdoc.html">unicode_properties</a>
    <li><a href="../ruby_3_3_0_preview2/COPYING.html">COPYING</a>
    <li><a href="../ruby_3_3_0_preview2/COPYING_ja.html">COPYING.ja</a>
    <li><a href="../ruby_3_3_0_preview2/LEGAL.html">LEGAL</a>
    <li><a href="../ruby_3_3_0_preview2/NEWS_md.html">NEWS</a>
    <li><a href="../ruby_3_3_0_preview2/README_ja_md.html">README.ja</a>
    <li><a href="../ruby_3_3_0_preview2/README_md.html">README</a>
    <li><a href="../security_rdoc.html">security</a>
    <li><a href="../signals_rdoc.html">signals</a>
    <li><a href="../standard_library_rdoc.html">standard_library</a>
    <li><a href="../strftime_formatting_rdoc.html">strftime_formatting</a>
    <li><a href="../syntax_rdoc.html">syntax</a>
    <li><a href="../syntax/assignment_rdoc.html">assignment</a>
    <li><a href="../syntax/calling_methods_rdoc.html">calling_methods</a>
    <li><a href="../syntax/comments_rdoc.html">comments</a>
    <li><a href="../syntax/control_expressions_rdoc.html">control_expressions</a>
    <li><a href="../syntax/exceptions_rdoc.html">exceptions</a>
    <li><a href="../syntax/literals_rdoc.html">literals</a>
    <li><a href="../syntax/methods_rdoc.html">methods</a>
    <li><a href="../syntax/miscellaneous_rdoc.html">miscellaneous</a>
    <li><a href="../syntax/modules_and_classes_rdoc.html">modules_and_classes</a>
    <li><a href="../syntax/pattern_matching_rdoc.html">pattern_matching</a>
    <li><a href="../syntax/precedence_rdoc.html">precedence</a>
    <li><a href="../syntax/refinements_rdoc.html">refinements</a>
    <li><a href="../timezones_rdoc.html">timezones</a>
    <li><a href="../windows_md.html">windows</a>
    <li><a href="../yjit/yjit_md.html">yjit</a>
    <li><a href="../yjit/yjit_hacking_md.html">yjit_hacking</a>
  </ul>
</div>

   </div>
  </div>
</nav>


<!--  carbon ads here -->

<div id='extraz'>
  <div class='adzbox-index'  >
   
  </div>
 </div>         


<main role="main" aria-label="Page syntax/calling_methods.rdoc">

<h1 id="label-Calling+Methods">Calling Methods<span><a href="#label-Calling+Methods">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>Calling a method sends a message to an object so it can perform some work.</p>

<p>In ruby you send a message to an object like this:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>()
</pre>

<p>Note that the parenthesis are optional:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>
</pre>

<p>Except when there is difference between using and omitting parentheses, this document uses parenthesis when arguments are present to avoid confusion.</p>

<p>This section only covers calling methods.  See also the <a href="methods_rdoc.html">syntax documentation on defining methods</a>.</p>

<h2 id="label-Receiver">Receiver<span><a href="#label-Receiver">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p><code>self</code> is the default receiver.  If you don’t specify any receiver <code>self</code> will be used.  To specify a receiver use <code>.</code>:</p>

<pre class="ruby"><span class="ruby-identifier">my_object</span>.<span class="ruby-identifier">my_method</span>
</pre>

<p>This sends the <code>my_method</code> message to <code>my_object</code>.  Any object can be a receiver but depending on the method’s visibility sending a message may raise a <a href="../NoMethodError.html"><code>NoMethodError</code></a>.</p>

<p>You may also use <code>::</code> to designate a receiver, but this is rarely used due to the potential for confusion with <code>::</code> for namespaces.</p>

<h3 id="label-Chaining+Method+Calls">Chaining Method Calls<span><a href="#label-Chaining+Method+Calls">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>You can “chain” method calls by immediately following one method call with another.</p>

<p>This example chains methods <a href="../Array.html#method-i-append"><code>Array#append</code></a> and <a href="../Array.html#method-i-compact"><code>Array#compact</code></a>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">:foo</span>, <span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-value">2</span>]
<span class="ruby-identifier">a1</span> = [<span class="ruby-value">:baz</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">:bam</span>, <span class="ruby-keyword">nil</span>]
<span class="ruby-identifier">a2</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">append</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">a1</span>).<span class="ruby-identifier">compact</span>
<span class="ruby-identifier">a2</span> <span class="ruby-comment"># =&gt; [:foo, &quot;bar&quot;, 2, :baz, :bam]</span>
</pre>

<p>Details:</p>
<ul><li>
<p>First method <code>merge</code> creates a copy of <code>a</code>, appends (separately) each element of <code>a1</code> to the copy, and returns</p>

<pre class="ruby">[<span class="ruby-value">:foo</span>, <span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-value">2</span>, <span class="ruby-value">:baz</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">:bam</span>, <span class="ruby-keyword">nil</span>]
</pre>
</li><li>
<p>Chained method <code>compact</code> creates a copy of that return value, removes its <code>nil</code>-valued entries, and returns</p>

<pre class="ruby">[<span class="ruby-value">:foo</span>, <span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-value">2</span>, <span class="ruby-value">:baz</span>, <span class="ruby-value">:bam</span>]
</pre>
</li></ul>

<p>You can chain methods that are in different classes. This example chains methods <a href="../Hash.html#method-i-to_a"><code>Hash#to_a</code></a> and <a href="../Array.html#method-i-reverse"><code>Array#reverse</code></a>:</p>

<pre class="ruby"><span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">to_a</span>.<span class="ruby-identifier">reverse</span> <span class="ruby-comment"># =&gt; [[:baz, 2], [:bar, 1], [:foo, 0]]</span>
</pre>

<p>Details:</p>
<ul><li>
<p>First method <a href="../Hash.html#method-i-to_a"><code>Hash#to_a</code></a> converts <code>a</code> to an Array, and returns</p>

<pre class="ruby">[[<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>], [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>], [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]]
</pre>
</li><li>
<p>Chained method <a href="../Array.html#method-i-reverse"><code>Array#reverse</code></a> creates copy of that return value, reverses it, and  returns</p>

<pre class="ruby">[[<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>], [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>], [<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>]]
</pre>
</li></ul>

<h3 id="label-Safe+Navigation+Operator">Safe Navigation Operator<span><a href="#label-Safe+Navigation+Operator">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p><code>&amp;.</code>, called “safe navigation operator”, allows to skip method call when receiver is <code>nil</code>. It returns <code>nil</code> and doesn’t evaluate method’s arguments if the call is skipped.</p>

<pre class="ruby"><span class="ruby-constant">REGEX</span> = <span class="ruby-regexp">/(ruby) is (\w+)/i</span>
<span class="ruby-string">&quot;Ruby is awesome!&quot;</span>.<span class="ruby-identifier">match</span>(<span class="ruby-constant">REGEX</span>).<span class="ruby-identifier">values_at</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>)
<span class="ruby-comment"># =&gt; [&quot;Ruby&quot;, &quot;awesome&quot;]</span>
<span class="ruby-string">&quot;Python is fascinating!&quot;</span>.<span class="ruby-identifier">match</span>(<span class="ruby-constant">REGEX</span>).<span class="ruby-identifier">values_at</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>)
<span class="ruby-comment"># NoMethodError: undefined method `values_at&#39; for nil:NilClass</span>
<span class="ruby-string">&quot;Python is fascinating!&quot;</span>.<span class="ruby-identifier">match</span>(<span class="ruby-constant">REGEX</span>)&amp;.<span class="ruby-identifier">values_at</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>)
<span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>This allows to easily chain methods which could return empty value. Note that <code>&amp;.</code> skips only one next call, so for a longer chain it is necessary to add operator on each level:</p>

<pre class="ruby"><span class="ruby-string">&quot;Python is fascinating!&quot;</span>.<span class="ruby-identifier">match</span>(<span class="ruby-constant">REGEX</span>)&amp;.<span class="ruby-identifier">values_at</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>).<span class="ruby-identifier">join</span>(<span class="ruby-string">&#39; - &#39;</span>)
<span class="ruby-comment"># NoMethodError: undefined method `join&#39; for nil:NilClass</span>
<span class="ruby-string">&quot;Python is fascinating!&quot;</span>.<span class="ruby-identifier">match</span>(<span class="ruby-constant">REGEX</span>)&amp;.<span class="ruby-identifier">values_at</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>)&amp;.<span class="ruby-identifier">join</span>(<span class="ruby-string">&#39; - &#39;</span>)
<span class="ruby-comment"># =&gt; nil</span>
</pre>

<h2 id="label-Arguments">Arguments<span><a href="#label-Arguments">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>There are three types of arguments when sending a message, the positional arguments, keyword (or named) arguments and the block argument.  Each message sent may use one, two or all types of arguments, but the arguments must be supplied in this order.</p>

<p>All arguments in ruby are passed by reference and are not lazily evaluated.</p>

<p>Each argument is separated by a <code>,</code>:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-string">&#39;2&#39;</span>, <span class="ruby-value">:three</span>)
</pre>

<p>Arguments may be an expression, a hash argument:</p>

<pre class="ruby"><span class="ruby-string">&#39;key&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">value</span>
</pre>

<p>or a keyword argument:</p>

<pre>key: value</pre>

<p><a href="../Hash.html"><code>Hash</code></a> and keyword arguments must be contiguous and must appear after all positional arguments, but may be mixed:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-string">&#39;a&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-value">1</span>, <span class="ruby-value">b:</span> <span class="ruby-value">2</span>, <span class="ruby-string">&#39;c&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-value">3</span>)
</pre>

<h3 id="label-Positional+Arguments">Positional Arguments<span><a href="#label-Positional+Arguments">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The positional arguments for the message follow the method name:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-identifier">argument1</span>, <span class="ruby-identifier">argument2</span>)
</pre>

<p>In many cases, parenthesis are not necessary when sending a message:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span> <span class="ruby-identifier">argument1</span>, <span class="ruby-identifier">argument2</span>
</pre>

<p>However, parenthesis are necessary to avoid ambiguity.  This will raise a <a href="../SyntaxError.html"><code>SyntaxError</code></a> because ruby does not know which method argument3 should be sent to:</p>

<pre>method_one argument1, method_two argument2, argument3</pre>

<p>If the method definition has a <code>*argument</code> extra positional arguments will be assigned to <code>argument</code> in the method as an <a href="../Array.html"><code>Array</code></a>.</p>

<p>If the method definition doesn’t include keyword arguments, the keyword or hash-type arguments are assigned as a single hash to the last argument:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-identifier">options</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">options</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">my_method</span>(<span class="ruby-string">&#39;a&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-value">1</span>, <span class="ruby-value">b:</span> <span class="ruby-value">2</span>) <span class="ruby-comment"># prints: {&#39;a&#39;=&gt;1, :b=&gt;2}</span>
</pre>

<p>If too many positional arguments are given, an <a href="../ArgumentError.html"><code>ArgumentError</code></a> is raised.</p>

<h3 id="label-Default+Positional+Arguments">Default Positional Arguments<span><a href="#label-Default+Positional+Arguments">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>When the method defines default arguments you do not need to supply all the arguments to the method.  Ruby will fill in the missing arguments in-order.</p>

<p>First we’ll cover the simple case where the default arguments appear on the right.  Consider this method:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span> = <span class="ruby-value">3</span>, <span class="ruby-identifier">d</span> = <span class="ruby-value">4</span>)
  <span class="ruby-identifier">p</span> [<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span>, <span class="ruby-identifier">d</span>]
<span class="ruby-keyword">end</span>
</pre>

<p>Here <code>c</code> and <code>d</code> have default values which ruby will apply for you.  If you send only two arguments to this method:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>)
</pre>

<p>You will see ruby print <code>[1, 2, 3, 4]</code>.</p>

<p>If you send three arguments:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">5</span>)
</pre>

<p>You will see ruby print <code>[1, 2, 5, 4]</code></p>

<p>Ruby fills in the missing arguments from left to right.</p>

<p>Ruby allows default values to appear in the middle of positional arguments. Consider this more complicated method:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span> = <span class="ruby-value">2</span>, <span class="ruby-identifier">c</span> = <span class="ruby-value">3</span>, <span class="ruby-identifier">d</span>)
  <span class="ruby-identifier">p</span> [<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span>, <span class="ruby-identifier">d</span>]
<span class="ruby-keyword">end</span>
</pre>

<p>Here <code>b</code> and <code>c</code> have default values.  If you send only two arguments to this method:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-value">4</span>)
</pre>

<p>You will see ruby print <code>[1, 2, 3, 4]</code>.</p>

<p>If you send three arguments:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-value">5</span>, <span class="ruby-value">6</span>)
</pre>

<p>You will see ruby print <code>[1, 5, 3, 6]</code>.</p>

<p>Describing this in words gets complicated and confusing. I’ll describe it in variables and values instead.</p>

<p>First <code>1</code> is assigned to <code>a</code>, then <code>6</code> is assigned to <code>d</code>.  This leaves only the arguments with default values.  Since <code>5</code> has not been assigned to a value yet, it is given to <code>b</code> and <code>c</code> uses its default value of <code>3</code>.</p>

<h3 id="label-Keyword+Arguments">Keyword Arguments<span><a href="#label-Keyword+Arguments">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Keyword arguments follow any positional arguments and are separated by commas like positional arguments:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-identifier">positional1</span>, <span class="ruby-value">keyword1:</span> <span class="ruby-identifier">value1</span>, <span class="ruby-value">keyword2:</span> <span class="ruby-identifier">value2</span>)
</pre>

<p>Any keyword arguments not given will use the default value from the method definition.  If a keyword argument is given that the method did not list, and the method definition does not accept arbitrary keyword arguments, an <a href="../ArgumentError.html"><code>ArgumentError</code></a> will be raised.</p>

<p>Keyword argument value can be omitted, meaning the value will be be fetched from the context by the name of the key</p>

<pre class="ruby"><span class="ruby-identifier">keyword1</span> = <span class="ruby-string">&#39;some value&#39;</span>
<span class="ruby-identifier">my_method</span>(<span class="ruby-identifier">positional1</span>, <span class="ruby-value">keyword1:</span>)
<span class="ruby-comment"># ...is the same as</span>
<span class="ruby-identifier">my_method</span>(<span class="ruby-identifier">positional1</span>, <span class="ruby-value">keyword1:</span> <span class="ruby-identifier">keyword1</span>)
</pre>

<p>Be aware that when method parenthesis are omitted, too, the parsing order might be unexpected:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span> <span class="ruby-identifier">positional1</span>, <span class="ruby-value">keyword1:</span>

<span class="ruby-identifier">some_other_expression</span>

<span class="ruby-comment"># ...is actually parsed as</span>
<span class="ruby-identifier">my_method</span>(<span class="ruby-identifier">positional1</span>, <span class="ruby-value">keyword1:</span> <span class="ruby-identifier">some_other_expression</span>)
</pre>

<h3 id="label-Block+Argument">Block Argument<span><a href="#label-Block+Argument">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The block argument sends a closure from the calling scope to the method.</p>

<p>The block argument is always last when sending a message to a method.  A block is sent to a method using <code>do ... end</code> or <code>{ ... }</code>:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span> <span class="ruby-keyword">do</span>
  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>or:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span> {
  <span class="ruby-comment"># ...</span>
}
</pre>

<p><code>do end</code> has lower precedence than <code>{ }</code> so:</p>

<pre class="ruby"><span class="ruby-identifier">method_1</span> <span class="ruby-identifier">method_2</span> {
  <span class="ruby-comment"># ...</span>
}
</pre>

<p>Sends the block to <code>method_2</code> while:</p>

<pre class="ruby"><span class="ruby-identifier">method_1</span> <span class="ruby-identifier">method_2</span> <span class="ruby-keyword">do</span>
  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Sends the block to <code>method_1</code>.  Note that in the first case if parentheses are used the block is sent to <code>method_1</code>.</p>

<p>A block will accept arguments from the method it was sent to.  Arguments are defined similar to the way a method defines arguments.  The block’s arguments go in <code>| ... |</code> following the opening <code>do</code> or <code>{</code>:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">argument1</span>, <span class="ruby-identifier">argument2</span><span class="ruby-operator">|</span>
  <span class="ruby-comment"># ...</span>
<span class="ruby-keyword">end</span>
</pre>

<h4 id="label-Block+Local+Arguments">Block Local Arguments<span><a href="#label-Block+Local+Arguments">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>You may also declare block-local arguments to a block using <code>;</code> in the block arguments list.  Assigning to a block-local argument will not override local arguments outside the block in the caller’s scope:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">place</span> = <span class="ruby-string">&quot;world&quot;</span>

<span class="ruby-identifier">my_method</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">obj</span>; <span class="ruby-identifier">place</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">place</span> = <span class="ruby-string">&quot;block&quot;</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;hello #{obj} this is #{place}&quot;</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;place is: #{place}&quot;</span>
</pre>

<p>This prints:</p>

<pre class="ruby"><span class="ruby-identifier">hello</span> <span class="ruby-identifier">main</span> <span class="ruby-identifier">this</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">block</span>
<span class="ruby-identifier">place</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">world</span>
</pre>

<p>So the <code>place</code> variable in the block is not the same <code>place</code> variable as outside the block.  Removing <code>; place</code> from the block arguments gives this result:</p>

<pre class="ruby"><span class="ruby-identifier">hello</span> <span class="ruby-identifier">main</span> <span class="ruby-identifier">this</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">block</span>
<span class="ruby-identifier">place</span> <span class="ruby-identifier">is</span> <span class="ruby-identifier">block</span>
</pre>

<h3 id="label-Array+to+Arguments+Conversion"><a href="../Array.html"><code>Array</code></a> to Arguments Conversion<span><a href="#label-Array+to+Arguments+Conversion">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Given the following method:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-identifier">argument1</span>, <span class="ruby-identifier">argument2</span>, <span class="ruby-identifier">argument3</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>You can turn an <a href="../Array.html"><code>Array</code></a> into an argument list with <code>*</code> (or splat) operator:</p>

<pre class="ruby"><span class="ruby-identifier">arguments</span> = [<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>]
<span class="ruby-identifier">my_method</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">arguments</span>)
</pre>

<p>or:</p>

<pre class="ruby"><span class="ruby-identifier">arguments</span> = [<span class="ruby-value">2</span>, <span class="ruby-value">3</span>]
<span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">arguments</span>)
</pre>

<p>Both are equivalent to:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>)
</pre>

<p>If the method accepts keyword arguments, the splat operator will convert a hash at the end of the array into keyword arguments:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>, <span class="ruby-value">c:</span> <span class="ruby-value">3</span>)
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">arguments</span> = [<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, { <span class="ruby-value">c:</span> <span class="ruby-value">4</span> }]
<span class="ruby-identifier">my_method</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">arguments</span>)
</pre>

<p>Note that this behavior is currently deprecated and will emit a warning. This behavior will be removed in Ruby 3.0.</p>

<p>You may also use the <code>**</code> (described next) to convert a <a href="../Hash.html"><code>Hash</code></a> into keyword arguments.</p>

<p>If the number of objects in the <a href="../Array.html"><code>Array</code></a> do not match the number of arguments for the method, an <a href="../ArgumentError.html"><code>ArgumentError</code></a> will be raised.</p>

<p>If the splat operator comes first in the call, parentheses must be used to avoid a warning:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span> <span class="ruby-operator">*</span><span class="ruby-identifier">arguments</span>  <span class="ruby-comment"># warning</span>
<span class="ruby-identifier">my_method</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">arguments</span>) <span class="ruby-comment"># no warning</span>
</pre>

<h3 id="label-Hash+to+Keyword+Arguments+Conversion"><a href="../Hash.html"><code>Hash</code></a> to Keyword Arguments Conversion<span><a href="#label-Hash+to+Keyword+Arguments+Conversion">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Given the following method:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-value">first:</span> <span class="ruby-value">1</span>, <span class="ruby-value">second:</span> <span class="ruby-value">2</span>, <span class="ruby-value">third:</span> <span class="ruby-value">3</span>)
<span class="ruby-keyword">end</span>
</pre>

<p>You can turn a <a href="../Hash.html"><code>Hash</code></a> into keyword arguments with the <code>**</code> (keyword splat) operator:</p>

<pre class="ruby"><span class="ruby-identifier">arguments</span> = { <span class="ruby-value">first:</span> <span class="ruby-value">3</span>, <span class="ruby-value">second:</span> <span class="ruby-value">4</span>, <span class="ruby-value">third:</span> <span class="ruby-value">5</span> }
<span class="ruby-identifier">my_method</span>(<span class="ruby-operator">**</span><span class="ruby-identifier">arguments</span>)
</pre>

<p>or:</p>

<pre class="ruby"><span class="ruby-identifier">arguments</span> = { <span class="ruby-value">first:</span> <span class="ruby-value">3</span>, <span class="ruby-value">second:</span> <span class="ruby-value">4</span> }
<span class="ruby-identifier">my_method</span>(<span class="ruby-value">third:</span> <span class="ruby-value">5</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">arguments</span>)
</pre>

<p>Both are equivalent to:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span>(<span class="ruby-value">first:</span> <span class="ruby-value">3</span>, <span class="ruby-value">second:</span> <span class="ruby-value">4</span>, <span class="ruby-value">third:</span> <span class="ruby-value">5</span>)
</pre>

<p>If the method definition uses the keyword splat operator to gather arbitrary keyword arguments, they will not be gathered by <code>*</code>:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">a</span>, <span class="ruby-operator">**</span><span class="ruby-identifier">kw</span>)
  <span class="ruby-identifier">p</span> <span class="ruby-value">arguments:</span> <span class="ruby-identifier">a</span>, <span class="ruby-value">keywords:</span> <span class="ruby-identifier">kw</span>
<span class="ruby-keyword">end</span>

<span class="ruby-identifier">my_method</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-string">&#39;3&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-value">4</span>, <span class="ruby-value">five:</span> <span class="ruby-value">6</span>)
</pre>

<p>Prints:</p>

<pre class="ruby">{<span class="ruby-value">:arguments</span><span class="ruby-operator">=&gt;</span>[<span class="ruby-value">1</span>, <span class="ruby-value">2</span>], <span class="ruby-value">:keywords</span><span class="ruby-operator">=&gt;</span>{<span class="ruby-string">&#39;3&#39;</span><span class="ruby-operator">=&gt;</span><span class="ruby-value">4</span>, <span class="ruby-value">:five</span><span class="ruby-operator">=&gt;</span><span class="ruby-value">6</span>}}
</pre>

<h3 id="label-Proc+to+Block+Conversion"><a href="../Proc.html"><code>Proc</code></a> to Block Conversion<span><a href="#label-Proc+to+Block+Conversion">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Given a method that use a block:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">my_method</span>
  <span class="ruby-keyword">yield</span> <span class="ruby-keyword">self</span>
<span class="ruby-keyword">end</span>
</pre>

<p>You can convert a proc or lambda to a block argument with the <code>&amp;</code> (block conversion) operator:</p>

<pre class="ruby"><span class="ruby-identifier">argument</span> = <span class="ruby-identifier">proc</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span><span class="ruby-operator">|</span> <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{a.inspect} was yielded&quot;</span> }

<span class="ruby-identifier">my_method</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">argument</span>)
</pre>

<p>If the block conversion operator comes first in the call, parenthesis must be used to avoid a warning:</p>

<pre class="ruby"><span class="ruby-identifier">my_method</span> <span class="ruby-operator">&amp;</span><span class="ruby-identifier">argument</span>  <span class="ruby-comment"># warning</span>
<span class="ruby-identifier">my_method</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">argument</span>) <span class="ruby-comment"># no warning</span>
</pre>

<h2 id="label-Method+Lookup"><a href="../Method.html"><code>Method</code></a> Lookup<span><a href="#label-Method+Lookup">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>When you send a message, Ruby looks up the method that matches the name of the message for the receiver.  Methods are stored in classes and modules so method lookup walks these, not the objects themselves.</p>

<p>Here is the order of method lookup for the receiver’s class or module <code>R</code>:</p>
<ul><li>
<p>The prepended modules of <code>R</code> in reverse order</p>
</li><li>
<p>For a matching method in <code>R</code></p>
</li><li>
<p>The included modules of <code>R</code> in reverse order</p>
</li></ul>

<p>If <code>R</code> is a class with a superclass, this is repeated with <code>R</code>‘s superclass until a method is found.</p>

<p>Once a match is found method lookup stops.</p>

<p>If no match is found this repeats from the beginning, but looking for <code>method_missing</code>.  The default <code>method_missing</code> is <a href="../BasicObject.html#method-i-method_missing"><code>BasicObject#method_missing</code></a> which raises a <a href="../NameError.html"><code>NameError</code></a> when invoked.</p>

<p>If refinements (an experimental feature) are active, the method lookup changes. See the <a href="refinements_rdoc.html">refinements documentation</a> for details.</p>

</main>

</div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

